package com.loongtech.bi.advertisment.controller;

import com.loongtech.bi.advertisment.model.ActiveTranData;
import com.loongtech.bi.advertisment.model.ChannelTotalData;
import com.loongtech.bi.advertisment.model.NewRemainData;
import com.loongtech.bi.advertisment.service.AdvDayDataGroupService;
import com.loongtech.core.util.Time;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import java.util.*;

/**
 * ClassName: AdvDayDataAction
 *
 * @author liuchen
 * @Description: 市场数据分析 —— 广告日数据（分渠道）
 * @date 2016-11-30 下午2:23:49
 */
@Controller("adDayReportGroupDetailAction")
@Scope("view")
public class AdvDayDataGroupDetailAction extends BaseAdvAction {
    private static final long serialVersionUID = 1L;

    private String dateRadio = "7"; // 切换的天数：1=昨天。7=近7天。15=近15天。30=近30天，0=自选时间

    private Date dateStart = new Date(Time.getBeginningOfDay(Time.currentTime() - 7 * Time.DAY)); // 自选时间：开始时间
    private Date dateEnd = new Date(Time.getBeginningOfDay(Time.currentTime() - Time.DAY)); // 自选时间：结束时间

    private List<NewRemainData> newRemainDatas;
    private List<List<Object>> totalPayDatas;
    private List<ActiveTranData> activeTranDatas;
    private List<ChannelTotalData> channelTotalData;

    @Resource(name = "advDayDataGroupService")
    private AdvDayDataGroupService service;

    @PostConstruct
    public void init() {
        search();
    }

    public void search() {
        FacesContext context = FacesContext.getCurrentInstance();
        if (hasAgents == null || hasAgents.size() <= 0) {
            context.addMessage(null, new FacesMessage("提示", "此用户无任何代理商权限，请联系管理员配置!"));
            return;
        }
        if ((selectedAgent == null || selectedAgent.length <= 0) && !"admin".equals(session.getUserName())) {
            context.addMessage(null, new FacesMessage("提示", "请选择代理商!"));
            return;
        }
        newRemainDatas = new ArrayList<>();
        totalPayDatas = new ArrayList<>();

        try {
            String startDay = Time.getDateStr(new Date(Time.getBeginningOfDay(("0".equals(dateRadio) ? dateStart
                    : new Date(Time.getBeginningOfDay(Time.currentTime() - (Integer.parseInt(dateRadio)) * Time.DAY)))
                    .getTime()))); // 开始时间
            String endDay = Time.getDateStr(new Date(Time.getBeginningOfDay(("0".equals(dateRadio) ? dateEnd
                    : new Date(Time.getBeginningOfDay(Time.currentTime() - Time.DAY))).getTime()))); // 结束时间

            List<List<String>> stotalPayDatas = service.getTotalPayDatas(startDay, endDay, selectedAffiliation,
                    selectedChannelName, selectedAgent, selectedPlatform, selectedPaytype, selectedAdvPosition);
            for (List<String> ls : stotalPayDatas) {
                List<Object> lo = new ArrayList<Object>();
                for (int i = 0; i < ls.size(); i++) {
                    if (i < 5) {
                        lo.add(ls.get(i));
                    } else {
                        lo.add(Integer.valueOf(ls.get(i)));
                    }
                }
                totalPayDatas.add(lo);
            }

            newRemainDatas = service.getNewRemainDatas(startDay, endDay, selectedAffiliation, selectedChannelName,
                    selectedAgent, selectedPlatform, selectedPaytype, selectedAdvPosition);
            activeTranDatas = service.getActiveTranDatas(startDay, endDay, selectedAffiliation, selectedChannelName,
                    selectedAgent, selectedPlatform, selectedPaytype, selectedAdvPosition);

            //拼接数据
            Map<String, ChannelTotalData> totalDatas = new HashMap<>(16);
            for (ActiveTranData activeTranData : activeTranDatas) {
                String key = activeTranData.getKey();
                ChannelTotalData channelTotalData = totalDatas.get(key);
                if (channelTotalData == null) {
                    channelTotalData = new ChannelTotalData();
                    channelTotalData.setAgent(activeTranData.getAgent());
                    channelTotalData.setDateStr(activeTranData.getDateStr());
                    channelTotalData.setChannelName(activeTranData.getChannelName());

                    totalDatas.put(key, channelTotalData);
                }

                channelTotalData.setActEquipment(activeTranData.getActEquipment());
                channelTotalData.setAgent(activeTranData.getAgent());
                channelTotalData.setChannelName(activeTranData.getChannelName());
                channelTotalData.setClkEquipment(activeTranData.getClkEquipment());
                channelTotalData.setCpa(activeTranData.getCpa());
                channelTotalData.setDateStr(activeTranData.getDateStr());
                channelTotalData.setDiscount(activeTranData.getDiscount());
                channelTotalData.setRegistercount(activeTranData.getNewAccount() == 0 ? 0 : activeTranData.getDiscount() / activeTranData.getNewAccount());
                channelTotalData.setNewActive(activeTranData.getNewActive());
                channelTotalData.setNewAccount(activeTranData.getNewAccount());
                channelTotalData.setNewEquipment(activeTranData.getNewEquipment());
                channelTotalData.setNewPay(activeTranData.getNewPay());
                channelTotalData.setNewPayUser(activeTranData.getNewPayUser());
                channelTotalData.setPayrate(activeTranData.getPayrate());
                channelTotalData.setRoi(activeTranData.getRoi());
                channelTotalData.setTotalpay(activeTranData.getTotalpay());
            }
            for (NewRemainData newRemainData : newRemainDatas) {
                String key = newRemainData.getKey();
                ChannelTotalData channelTotalData = totalDatas.get(key);
                if (channelTotalData == null) {
                    channelTotalData = new ChannelTotalData();
                    channelTotalData.setAgent(newRemainData.getAgent());
                    channelTotalData.setDateStr(newRemainData.getDateStr());
                    channelTotalData.setChannelName(newRemainData.getChannelName());

                    totalDatas.put(key, channelTotalData);
                }
                channelTotalData.setRemain2(newRemainData.getRemain2());
                channelTotalData.setRemain3(newRemainData.getRemain3());
                channelTotalData.setRemain4(newRemainData.getRemain4());
                channelTotalData.setRemain5(newRemainData.getRemain5());
                channelTotalData.setRemain6(newRemainData.getRemain6());
                channelTotalData.setRemain7(newRemainData.getRemain7());
                channelTotalData.setRemain14(newRemainData.getRemain14());
                channelTotalData.setRemain30(newRemainData.getRemain30());
                channelTotalData.setRemain60(newRemainData.getRemain60());
                channelTotalData.setRemain90(newRemainData.getRemain90());
            }
            for (List<Object> totalPayData : totalPayDatas) {
                String key = totalPayData.get(0).toString() + (StringUtils.isEmpty(totalPayData.get(3).toString()) ? totalPayData.get(3).toString() : totalPayData.get(3).toString().toLowerCase()) + totalPayData.get(4).toString();
                ChannelTotalData channelTotalData = totalDatas.get(key);
                if (channelTotalData == null) {
                    channelTotalData = new ChannelTotalData();
                    channelTotalData.setAgent(totalPayData.get(4).toString());
                    channelTotalData.setDateStr(totalPayData.get(0).toString());
                    channelTotalData.setChannelName(totalPayData.get(3).toString());

                    totalDatas.put(key, channelTotalData);
                }
                channelTotalData.setIncome1(totalPayData.get(6).toString());
                channelTotalData.setIncome2(totalPayData.get(7).toString());
                channelTotalData.setIncome3(totalPayData.get(8).toString());
                channelTotalData.setIncome4(totalPayData.get(9).toString());
                channelTotalData.setIncome5(totalPayData.get(10).toString());
                channelTotalData.setIncome6(totalPayData.get(11).toString());
                channelTotalData.setIncome7(totalPayData.get(12).toString());
                channelTotalData.setIncome14(totalPayData.get(19).toString());
                channelTotalData.setIncome30(totalPayData.get(35).toString());
                channelTotalData.setIncome60(totalPayData.get(65).toString());
                channelTotalData.setIncome90(totalPayData.get(95).toString());
            }
            channelTotalData = new ArrayList<>(totalDatas.values());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String getDateRadio() {
        return dateRadio;
    }

    public void setDateRadio(String dateRadio) {
        this.dateRadio = dateRadio;
    }

    public Date getDateStart() {
        return dateStart;
    }

    public void setDateStart(Date dateStart) {
        this.dateStart = dateStart;
    }

    public Date getDateEnd() {
        return dateEnd;
    }

    public void setDateEnd(Date dateEnd) {
        this.dateEnd = dateEnd;
    }

    public List<NewRemainData> getNewRemainDatas() {
        return newRemainDatas;
    }

    public void setNewRemainDatas(List<NewRemainData> newRemainDatas) {
        this.newRemainDatas = newRemainDatas;
    }

    public List<List<Object>> getTotalPayDatas() {
        return totalPayDatas;
    }

    public void setTotalPayDatas(List<List<Object>> totalPayDatas) {
        this.totalPayDatas = totalPayDatas;
    }

    public List<ChannelTotalData> getChannelTotalData() {
        return channelTotalData;
    }

    public void setChannelTotalData(List<ChannelTotalData> channelTotalData) {
        this.channelTotalData = channelTotalData;
    }
}
